package com.qiguliuxing.dts.db.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiguliuxing.dts.db.dao.DtsIssueMapper;
import com.qiguliuxing.dts.db.domain.DtsIssue;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DtsIssueService {
	@Resource
	private DtsIssueMapper issueMapper;

	public List<DtsIssue> query() {
		QueryWrapper<DtsIssue> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsIssue::getDeleted, false);
		return issueMapper.selectList(queryWrapper);
	}

	public void deleteById(Integer id) {
		issueMapper.deleteById(id);
	}

	public void add(DtsIssue issue) {
		issue.setAddTime(LocalDateTime.now());
		issue.setUpdateTime(LocalDateTime.now());
		issueMapper.insert(issue);
	}

	public List<DtsIssue> querySelective(String question, Integer page, Integer size, String sort, String order) {
		QueryWrapper<DtsIssue> queryWrapper = new QueryWrapper<>();
		if (StrUtil.isNotEmpty(question)) {
			queryWrapper.lambda().like(DtsIssue::getQuestion, question);
		}
		queryWrapper.lambda().eq(DtsIssue::getDeleted, false);
		if (StrUtil.isNotEmpty(sort) && StrUtil.isNotEmpty(order)) {
			queryWrapper.orderByDesc(sort);
		}
		IPage<DtsIssue> iPage = new Page<>(page, size);
		return issueMapper.selectPage(iPage, queryWrapper).getRecords();
	}

	public int updateById(DtsIssue issue) {
		issue.setUpdateTime(LocalDateTime.now());
		return issueMapper.updateById(issue);
	}

	public DtsIssue findById(Integer id) {
		return issueMapper.selectById(id);
	}
}
